package com.leo;

import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Shape3D;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;

import java.util.function.Consumer;

/**
 * @ClassName MaterialDemo
 * @Description
 * @Author wangss
 * @date 2020.07.04 19:22
 * @Version 1.0
 */
public class MaterialDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();

        HBox hBox = new HBox(100);

        Box box = new Box(150, 150, 150);

        Cylinder cylinder = new Cylinder(50, 200, 64);

        Point3D point3D = new Point3D(1, 0, 0);
        cylinder.setRotationAxis(point3D);
        cylinder.setRotate(60);

        Sphere sphere = new Sphere(100, 64);

        hBox.getChildren().addAll(box, cylinder, sphere);

        hBox.getChildren().forEach(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                Shape3D shape3D = (Shape3D) node;
                PhongMaterial phongMaterial = new PhongMaterial();
                // 散射
                phongMaterial.setDiffuseColor(Color.DARKGREEN);
                // 高光
                phongMaterial.setSpecularColor(Color.AQUA);
//                phongMaterial.setSpecularPower();

                // 漫反射放图片
//                phongMaterial.setDiffuseMap(new Image(""));
                // 贴图
                phongMaterial.setBumpMap(new Image("/images/fx.jpg"));

                // 本身发光
//                phongMaterial.setSelfIlluminationMap();

                shape3D.setMaterial(phongMaterial);
            }
        });

        an.setStyle("-fx-background-color: #FFFFFF00");
        an.getChildren().addAll(hBox);
        an.setDepthTest(DepthTest.ENABLE);
        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 300d);

        Scene scene = new Scene(an, 1200, 800, true, SceneAntialiasing.BALANCED);
        scene.setCamera(new PerspectiveCamera());

        primaryStage.setScene(scene);
        primaryStage.setTitle("3D介绍");
        primaryStage.setWidth(1200);
        primaryStage.setHeight(800);
        primaryStage.show();
    }
}
